<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>XPKIT Source Formatter</title>
<link rel='stylesheet' type='text/css' href='xpkit_doc.css' />
</head>

<body>
<table cellpadding=20 border=0>
<tr><td>

<?php

$copyright = " *
 * Copyright(c) 2006 Brian Chung
 * All rights reserved
 * This program is freely distributable. Use it a your own risk.";

$cwd = str_replace ('\\', '/', getcwd());
$input_dir = $_GET["input_dir"];
$output_dir = $_GET["output_dir"];

if ($input_dir == "" || $output_dir == "") {
	$input_dir = "$cwd/..";
	$output_dir = "$cwd/xpkit";
?>

<h2>XPKIT Source Formatter</h2>
<form action="<?php print $_SERVER['PHP_SELF']; ?>">
<label>Input Directory:</label>
<input type="text" name="input_dir" value='<?php print $input_dir; ?>' size=50><br><br>
<label>Output Directory:</label>
<input type="text" name="output_dir" value='<?php print $output_dir; ?>' size=50><br><br>
<input type="submit" value="Format Source">
</form>

<?php
}
else {
	if (!is_dir($output_dir) && 
	    mkdir ($output_dir, 0700, true) == false) {
		print "ERROR: Failed to create the directory - $output_dir. Cannot proceed<br>";
	}
	else {
		format_directory ($input_dir, "");
		print "Done formatting source codes<br>";
	}
}

function format_directory ($dir, $subdir)
{
	global $output_dir;

	$handle = opendir ($dir);
	if ($handle == false) return false;

	$pattern = '/\\.(h|c|cc|h|cpp)$/';

	if ($subdir == "tools") return false;

	if ($subdir != "") {
		$target_dir = "$output_dir/$subdir";
		if (!is_dir($target_dir) && 
		    mkdir ($target_dir, 0700, true) == false) {
			print "ERROR: Failed to create the directory - $target_dir.<br>";
		}
	}

	while (($file = readdir($handle)) != false) {
		if ($file == "CVS" || $file == "." || $file == "..") continue;

		$full = "$dir/$file";
		if (is_dir($full)) {
			if ($subdir == "") $tmp = $file;
			else $tmp = $subdir . "/" . $file;
			format_directory ($full, $tmp);
		}
		else {
			if (preg_match($pattern, $file) == 0) continue;
			format_source ($full, $subdir, $file);
		}
	}

	closedir ($handle);
}

function format_source ($full, $subdir, $file)
{
	global $output_dir;
	global $copyright;

	$target_dir = "$output_dir/$subdir";
	$final = "$target_dir/$file";

	$src = fopen($full, "rb");
	if ($src == false) return false;

	$dst = fopen($final, "wb");
	if ($dst == false) {
		fclose ($src);
		return false;
	}

	$stage = 0;
	while (!feof($src)) {
		# fgets may not read an entire line 
		# if you php is earlier than 4.3
		$line = fgets ($src);

		if ($stage == 0) {
			$stage++;
			fwrite ($dst, $line);
		}
		else if ($stage == 1) {
			if (preg_match('/^ \\* \\$Id: fmtsrc.php,v 1.2 2006-03-08 03:48:31 bacon Exp $line) > 0) {
				fwrite ($dst, $line);
				fwrite ($dst, "$copyright\r\n");
			}
			else fwrite ($dst, $line);
			$stage++;
		}
		else {
			fwrite ($dst, $line);
		}
	}
	
	fclose ($src);
	fclose ($dst);

	print "Created $final...<br>";

	return true;
}

?>

</td></tr>
</table>
</body>
</html>
